/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.block;

import org.statewalker.parser.wiki.tokenizer.base.StreamToken;
import org.statewalker.tokenizer.base.AbstractTokenizer;
import org.statewalker.tokenizer.base.ITokenFactory;
import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;

public class HeaderTokenizer extends AbstractTokenizer<StreamToken> {

    public HeaderTokenizer(ITokenFactory<StreamToken> factory) {
        super(factory);
    }

    protected boolean checkChar(char ch) {
        return ch == '=';
    }

    public StreamToken read(ICharStream stream) {
        char ch = stream.getChar();
        if (!checkChar(ch))
            return null;
        boolean result = false;
        IMarker marker = stream.markPosition();
        try {
            stream.incPos();
            ch = stream.getChar();
            int counter = 1;
            while (checkChar(ch) && counter < 6) {
                result = true;
                stream.incPos();
                ch = stream.getChar();
                counter++;
            }
            return result ? newToken(stream, marker) : null;
        } finally {
            marker.close(!result);
        }
    }
}